package com.songqintuan.platform.navigation;

import java.util.Iterator;
import java.util.List;

import javax.faces.application.Application;

import javax.faces.bean.ManagedBean;

import javax.faces.bean.RequestScoped;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.richfaces.component.UIPanelMenu;

import org.richfaces.component.UIPanelMenuGroup;

import org.richfaces.component.UIPanelMenuItem;

import com.songqintuan.common.util.UIComponentUtil;

@ManagedBean(name="pMenu")
@RequestScoped
public class PMenu {

	private UIPanelMenu menu = null;

	public PMenu() {

		FacesContext fc = FacesContext.getCurrentInstance();

		Application application = FacesContext.getCurrentInstance()
				.getApplication();

		menu = (UIPanelMenu) application.createComponent(fc,
				UIPanelMenu.COMPONENT_TYPE, "org.richfaces.PanelMenuRenderer");
		UIPanelMenuGroup group = (UIPanelMenuGroup) application
				.createComponent(fc, UIPanelMenuGroup.COMPONENT_TYPE,

				"org.richfaces.PanelMenuGroupRenderer");

		group.setLabel("group1");

		group.setId("gr1");

		UIPanelMenuItem item = (UIPanelMenuItem) application.createComponent(
				fc, UIPanelMenuItem.COMPONENT_TYPE,

				"org.richfaces.PanelMenuItemRenderer");

		item.setLabel("item1");

		item.setId("itm1");

		group.getChildren().add(item);

		item = (UIPanelMenuItem) application.createComponent(fc,
				UIPanelMenuItem.COMPONENT_TYPE,

				"org.richfaces.PanelMenuItemRenderer");

		item.setLabel("item2");

		item.setId("itm2");
		//item.addActionListener(UIComponentUtil.createActionListener("#{pMenu.select}"));
		item.setActionExpression(UIComponentUtil.createAction("#{pMenu.goUrl}", String.class));
		item.setImmediate(true);
		group.getChildren().add(item);

		menu.getChildren().add(group);

		group = (UIPanelMenuGroup) application.createComponent(fc,
				UIPanelMenuGroup.COMPONENT_TYPE,

				"org.richfaces.PanelMenuGroupRenderer");

		group.setLabel("group2");

		group.setId("gr2");
		
		UIPanelMenuGroup sgroup = (UIPanelMenuGroup) application.createComponent(fc,
				UIPanelMenuGroup.COMPONENT_TYPE,

				"org.richfaces.PanelMenuGroupRenderer");
		sgroup.setLabel("group2-1");
		sgroup.setId("group2-1");

		item = (UIPanelMenuItem) application.createComponent(fc,
				UIPanelMenuItem.COMPONENT_TYPE,

				"org.richfaces.PanelMenuItemRenderer");

		item.setLabel("item3");

		item.setId("itm3");
		sgroup.getChildren().add(item);
		group.getChildren().add(sgroup);

		item = (UIPanelMenuItem) application.createComponent(fc,
				UIPanelMenuItem.COMPONENT_TYPE,

				"org.richfaces.PanelMenuItemRenderer");

		item.setLabel("item4");

		item.setId("itm4");

		group.getChildren().add(item);

		menu.getChildren().add(group);

		item = (UIPanelMenuItem) application.createComponent(fc,
				UIPanelMenuItem.COMPONENT_TYPE,

				"org.richfaces.PanelMenuItemRenderer");

		item.setLabel("item5");

		item.setId("itm5");

		menu.getChildren().add(item);
		List<UIComponent> aa =menu.getChildren();
		Iterator<UIComponent> it = aa.iterator();
		while (it.hasNext()){
			System.out.println(it.next().getId());
		}

	}

	public void setMenu(UIPanelMenu menu) {

		this.menu = menu;

	}

	public UIPanelMenu getMenu() {

		return menu;

	}
	public void select(ActionEvent event) {  
        System.out.println(event.getComponent().getId());  
    }
    public String goUrl(){
    	System.out.println("gogogogo");
    	return null;
    }

}
